package com.dongao.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

import com.dongao.model.Mp3Info;

public class FileUtil {
	public static final String FILENAME_EXTENSION_MP3=".mp3";
	public static final String FILENAME_EXTENSION_MP4=".mp4";
	public static final String FILENAME_EXTENSION_WMV=".wmv";
	public static final String FILENAME_EXTENSION_AVI=".avi";
	public static final String FILENAME_EXTENSION_M3U8=".m3u8";
	/**
	 * 获得SD卡的根目录
	 * @return
	 */
	public static String getSDCardRoot() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	
	/**
	 * 在SD卡上创建文件
	 * @throws IOException 
	 */
	public static File createFileInSDCard(String fileName, String dir) throws IOException {
		File file = new File(getSDCardRoot() + dir + File.separator + fileName);
		System.out.println("文件："+ getSDCardRoot() + dir + File.separator + fileName);
		file.createNewFile();
		return file;
	}
	
	/**
	 * 在SD卡上创建目录
	 * @param dir
	 */
	public static File createSDDir(String dir) {
		File dirFile = new File(getSDCardRoot() + dir + File.separator);
		return dirFile;
	}
	
	/**
	 * 判断SD卡上的文件是否存在
	 * @param fileName
	 * @param path
	 */
	public static boolean isFileExist(String fileName, String path) {
		File file = new File(getSDCardRoot() + path + File.separator + fileName);
		return file.exists();
	}
	
	/**
	 * 将一个InputStream里面的数据写入SD卡
	 * @param path
	 * @param fileName
	 * @param input
	 */
	public static File write2SDFromInput(String path, String fileName, InputStream input) {
		File file = null;
		OutputStream output = null;
		createSDDir(path);
		try {
			file = createFileInSDCard(fileName, path);
			output = new FileOutputStream(file);
			byte buffer[] = new byte[4 * 1024];
			int temp;
			while((temp = input.read(buffer)) != -1) {
				output.write(buffer,0,temp);
			}
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(output!=null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}
	
	/**
	 * 读取目录中的指定格式文件的名字和大小
	 */
	public static List<Mp3Info> getMp3Files(String path) {
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		File file = new File(getSDCardRoot() + File.separator + path);
		File[] files = file.listFiles();
		
		if (files != null &&files.length != 0) {
			for (File f : files) {
				if(!f.getName().endsWith(FILENAME_EXTENSION_M3U8)) {
					continue;
				}
				
				Mp3Info mp3Info = new Mp3Info();
				mp3Info.setMp3Name(f.getName());
				mp3Info.setMp3Size(String.valueOf(f.length()));
				mp3Infos.add(mp3Info);
			}
		}
		
		return mp3Infos;
	}
	
	/**
	 * 读取制定目录下的文件，将内容以字符串形式返回
	 */
	public static String readFileAsString(String path) {
		String content="";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
			String line="";
			while((line=br.readLine())!=null) {
				content+=line;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return content;
	}
	
}
